﻿using M3u8Puller.Helper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace M3u8Puller.Config
{
    public class AppConfig
    {
        /// <summary>
        /// 默认保存目录
        /// </summary>
        public string DefaultDir { get; set; } = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);


        public static AppConfig GetConfig()
        {
            var appConfig = new AppConfig();
            var fileName = AppDomain.CurrentDomain.BaseDirectory + "AppConfig.json";
            if (File.Exists(fileName))
            {
                try
                {
                    var json = File.ReadAllText(fileName);
                    appConfig = JsonConvert.DeserializeObject<AppConfig>(json);
                }
                catch (Exception ex)
                {
                    LogHelper.LocalSaveLog("读取配置文件失败，" + ex.Message);
                }
            }
            return appConfig;
        }

        public static bool SaveConfig(AppConfig appConfig)
        {
            bool bo = false;
            var fileName = AppDomain.CurrentDomain.BaseDirectory + "AppConfig.json";
            try
            {
                if (!File.Exists(fileName))
                {
                    var st = File.CreateText(fileName);
                    st.Write(JsonConvert.SerializeObject(appConfig));
                    st.Dispose();
                }
                else
                {
                    File.WriteAllText(fileName, JsonConvert.SerializeObject(appConfig));
                }
                bo = true;
            }
            catch (Exception ex)
            {
                LogHelper.LocalSaveLog("保存配置文件失败，" + ex.Message);
            }
            return bo;
        }
    }
}
